package com.company.service;

import com.company.dao.PageProjectDao;
import com.company.entity.Projects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PageProjectService {
    @Autowired
    PageProjectDao pageProjectDao;

    private static final int NUM = 4;

    public List<Projects> query(int pageNum) {
        Pageable pageable = new PageRequest(pageNum, NUM, Sort.Direction.DESC);
        return pageProjectDao.findAll(pageable).getContent();
    }

    public int queryPageNum() {
        int sum = pageProjectDao.findAll().size();
        return sum % NUM == 0 ? sum / NUM : sum / NUM + 1;
    }
}
